package com.vito.dcs.water.device.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vito.dcs.asphalt.device.dao.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.vito.base.factory.IDFactory;
import com.vito.base.model.DataGrid;
import com.vito.base.util.Validata;
import com.vito.dcs.water.device.dao.WaterBlenderMapper;
import com.vito.dcs.water.device.dao.WaterPoreMapper;
import com.vito.dcs.water.device.dao.WaterStationMapper;
import com.vito.dcs.water.device.model.WaterBlenderModel;
import com.vito.dcs.water.device.model.WaterStationModel;
import com.vito.dcs.water.device.service.WaterBlenderService;

/**
 * @program: WaterBlenderServiceImpl
 * @description: 水稳搅拌机设备设置表服务接口实现类
 * @author: rongr
 * @create: 2019/04/10
 */
@Service
public class WaterBlenderServiceImpl implements WaterBlenderService {

    @Autowired
    private WaterBlenderMapper waterBlenderMapper;
    @Autowired
    private WaterPoreMapper waterPoreMapper;
    @Autowired
    private WaterStationMapper waterStationMapper;

    @Override
    public Integer saveWaterBlender(Map<String, Object> map) {
        //创建ID
        String waterBlenderId = IDFactory.getIDFactory().getIdInc("tbl_dcs_min_water_blender") + "";
        map.put("waterBlenderId", waterBlenderId);
        //添加tbl_dcs_min_water_blender表
        //处理水稳From表单
        //MapUtil.mapWaterFrom(map);
        int blenderRes = waterBlenderMapper.saveWaterBlender(map);
        //拼接并添加tbl_dcs_min_water_station表code_num编码
        Map<String, String> codeMap = waterBlenderMapper.queryStationCodeById(map);
        String projectNo = codeMap.get("projectNo");
        String sectionNo = codeMap.get("sectionNo");
        String stationNo = codeMap.get("stationNo");
        String codeNum = projectNo + "-" + sectionNo + "-" + stationNo + "-" + map.get("machineNo");
        WaterStationModel waterStationModel = new WaterStationModel();
        waterStationModel.setWaterStationId(map.get("stationId").toString());
        waterStationModel.setCodeNum(codeNum);
        waterStationMapper.updateWaterStation(waterStationModel);
        if (blenderRes > 0) {
            //获取并添加tbl_dcs_min_water_pore表信息
            String poreParam = map.get("arr_obj_json").toString();
            if (!Validata.isNullOrEmpty(poreParam)) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) JSONArray.parse(poreParam);
                for (Map<String, Object> m : list) {
                    //水稳table框
                   // MapUtil.mapWaterTable(m);
                    String waterPoreId = IDFactory.getIDFactory().getIdInc("tbl_dcs_min_water_pore") + "";
                    m.put("waterPoreId", waterPoreId);
                    m.put("blenderId", waterBlenderId);
                    waterPoreMapper.saveWaterPore(m);
                }
            }
        }
        return blenderRes;
    }

    @Override
    public Integer updateWaterBlender(Map<String, Object> map) {
        //处理水稳From表单
        //MapUtil.mapWaterFrom(map);
        int updblenderRes = waterBlenderMapper.updateWaterBlender(map);
        //拼接并添加tbl_dcs_min_water_station表code_num编码
        Map<String, String> codeMap = waterBlenderMapper.queryStationCodeById(map);
        String projectNo = codeMap.get("projectNo");
        String sectionNo = codeMap.get("sectionNo");
        String stationNo = codeMap.get("stationNo");
        String codeNum = projectNo + "-" + sectionNo + "-" + stationNo + "-" + map.get("machineNo");
        WaterStationModel waterStationModel = new WaterStationModel();
        waterStationModel.setWaterStationId(map.get("stationId").toString());
        waterStationModel.setCodeNum(codeNum);
        waterStationMapper.updateWaterStation(waterStationModel);
        if (updblenderRes > 0) {
            String poreParam = map.get("arr_obj_json").toString();
            if (!Validata.isNullOrEmpty(poreParam)) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) JSONArray.parse(poreParam);
                for (Map<String, Object> m : list) {
                    //水稳table框
                   // MapUtil.mapWaterTable(m);
                    m.put("waterBlenderId", map.get("waterBlenderId"));
                    waterPoreMapper.updateWaterPore(m);
                }
            }
        }
        return updblenderRes;
    }

    @Override
    public WaterBlenderModel queryWaterBlenderById(Map<String, Object> map) {
        return waterBlenderMapper.queryWaterBlenderById(map);
    }

    @Override
    public DataGrid<Map<String, Object>> queryBlenderByPage(Map<String, Object> map) {
        DataGrid<Map<String, Object>> dg = new DataGrid<Map<String, Object>>();
        dg.setTotal(waterBlenderMapper.queryBlenderByPageCount(map));
        dg.setRows(waterBlenderMapper.queryBlenderByPage(map));
        return dg;
    }

    @Override
    public Integer delBlenderModel(Map<String, Object> map) {
        return waterBlenderMapper.delBlenderModel(map);
    }

	@Override
	public void updateIsCurrent(Map<String, Object> map) {
	/*	String isCurrent=map.get("isCurrent").toString();
		if("0".equals(isCurrent)){
			waterBlenderMapper.updateWaterBlender(map);
		}else if("1".equals(isCurrent)){
			if(waterBlenderMapper.updateWaterBlender(map)>0){
				waterBlenderMapper.updateIsCurrent(map);
			}
		}*/
        if(waterBlenderMapper.updateWaterBlender(map)>0){
            waterBlenderMapper.updateIsCurrent(map);
        }
	}
	@Override
	public List<HashMap<String, String>> findBlenderBySectionId(Map<String, String> map) {
		return waterBlenderMapper.findBlenderBySectionId(map);
	}

    @Override
    public String queryWaterLayerIdMachineNo(Map<String, String> map) {
        return waterBlenderMapper.queryWaterLayerIdMachineNo(map);
    }

}

